<?php
    class DetailsModel
    {
        public function __construct()
        {
    		$this->db = Yaf_Registry::get('db');
            $this->time = date("Y-m-d",time());
        }

        public function selectPushUser()
        {
            $sql = "select distinct  user_id from push_log";
            return $this->db->query($sql);
        }

        public function selectPushSites()
        {
            $sql = "select distinct  siteid from push_log";
            return $this->db->query($sql);
        }

        public function selectPushTime()
        {
            $sql = "select distinct FROM_UNIXTIME(create_time,'%Y-%m-%d') as date from push_log ";
            return $this->db->query($sql);
        }

        public function selectPushTimeInfo($userInfo,$timeInfo,$post)
        {

           // $todayTime = $this->time;
            $result = array();
            $i      = 0;
            foreach ($userInfo as $key => $value) {

               // foreach ($siteInfo as $k => $v) {

                    foreach ($timeInfo as $ks => $vs) {

                        if($vs['date'] == $post['push_time_input']){
                           // echo "11";
                            $next_day = strtotime($post['push_time_input']) + 3600*24;
                            $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']}  and `create_time`> UNIX_TIMESTAMP('{$post['push_time_input']}') and `create_time`<'{$next_day}'";
                            if($this->db->query($sql)[0]['count'] != "0"){
                                $result[$i++] = $this->db->query($sql); 
                            }
                            
                        }
                    }
                //}
            }
            //var_dump($result);
            return $result;
        }

        public function selectPushInfo($userInfo,$timeInfo)
        {
            $todayTime = $this->time;
            $result = array();
            $i      = 0;
            foreach ($userInfo as $key => $value) {

               // foreach ($siteInfo as $k => $v) {

                    foreach ($timeInfo as $ks => $vs) {

                        if($vs['date'] == $todayTime){
                           // echo "11";
                            $next_day = strtotime($this->time) + 3600*24;
                            $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']}  and `create_time`> UNIX_TIMESTAMP('{$this->time}') and `create_time`<'{$next_day}'";
                            if($this->db->query($sql)[0]['count'] != "0"){
                                $result[$i++] = $this->db->query($sql); 
                            }
                        }
                    }
                //}
            }
            //var_dump($result);
            return $result;
        }

        public function selectPushToday($userInfo,$siteInfo,$timeInfo)
        {
            $todayTime = $this->time;
            $result = array();
            $i      = 0;
            foreach ($userInfo as $key => $value) {

                foreach ($siteInfo as $k => $v) {

                    foreach ($timeInfo as $ks => $vs) {

                        if($vs['date'] == $todayTime){
                           // echo "11";
                            $next_day = strtotime($this->time) + 3600*24;
                            $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']} and siteid = {$v['siteid']} and `create_time`> UNIX_TIMESTAMP('{$this->time}') and `create_time`<'{$next_day}'";
                            if($this->db->query($sql)[0]['count'] != "0"){
                                $result[$i++] = $this->db->query($sql); 
                            }
                        }
                    }
                }
            }
            //var_dump($result);
            return $result;
        }

        public function selectPushUserAboutInfo($user_id,$sites,$time,$create_time)
        {
            //var_dump($this->time);
            $result = array();
            $i      = 0;
            foreach ($user_id as $key => $value) {
                foreach ($sites as $k => $v) {
                    if(is_array($time) == true){
                        foreach ($time as $ks => $vs) {
                            $next_day = strtotime($vs['date']) + 3600*24;
                            $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']} and siteid = {$v['siteid']} and create_time > UNIX_TIMESTAMP('{$vs['date']}') and create_time  < '{$next_day}' ";
                            if($this->db->query($sql)[0]['count'] != "0"){
                                $result[$i++] = $this->db->query($sql); 
                            }
                        } 
                    }
                    else if(is_array($time) == false && is_int($time) == false){
                        $next_day = strtotime($time) + 3600*24;
                        $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']} and siteid = {$v['siteid']} and create_time > UNIX_TIMESTAMP('{$time}') and create_time  < '{$next_day}' ";
                        if($this->db->query($sql)[0]['count'] != "0"){
                            $result[$i++] = $this->db->query($sql); 
                        }
         
                    }
                    else if(is_int($time) == true && $time == $value['user_id']){
                        foreach ($create_time as $ks => $vs) {
                        $next_day = strtotime($vs['date']) + 3600*24;
                            $sql = "select count(DISTINCT article_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']}  and siteid = {$v['siteid']} and create_time > UNIX_TIMESTAMP('{$vs['date']}') and create_time  < '{$next_day}'";
                            if($this->db->query($sql)[0]['count'] != "0"){
                                $result[$i++] = $this->db->query($sql); 
                            }
                       
                        }
                    } 
                }  
            }
            return $result;
        }

        public function selectPushUserAndTime($user_id,$sites,$push_user,$push_time,$create_time)
        {
            $result = array();
            $i      = 0;
            foreach ($user_id as $key => $value) {
                foreach ($sites as $k => $v) {
                    if(is_int($push_user) == true && is_string($push_time) == true){
                        foreach ($create_time as $ks => $vs) {
                            $next_day = strtotime($vs['date']) + 3600*24;
                            if($push_user == $value['user_id'] && $push_time == $vs['date']){
                                $sql = "select count(user_id) as count,user_id,siteid,create_time from push_log where user_id = {$value['user_id']}  and siteid = {$v['siteid']} and create_time > UNIX_TIMESTAMP('{$vs['date']}') and create_time  < '{$next_day}'";
                            
                                if($this->db->query($sql)[0]['count'] != "0"){
                                    $result[$i++] = $this->db->query($sql); 
                                }
                            }
                        }
                    }
                }
            }
            return $result;
        }
       
    }
?>